const db = require("../config/config");
const jsonWebToken = require("jsonwebtoken");
const SECRET_KEY = "kite1874";
module.exports = {
    getAllData(req, res) {
        db.dbMysql(`SELECT * FROM promotion`, null, function (err, data) {
            console.log(data);
           if(data){
            data.forEach(item => {
                item.key = item.promotionId
            });
            const data1 = data//抢购数据
            res.json({
                code: 200,
                msg: '查询成功',
                data: data1
            })
           }
        })
    },
    getAppointData(req, res) {
        console.log(req.body);
        db.dbMysql(`SELECT * FROM promotion WHERE promotionTitle like "%${req.body.promotionTitle}%" AND productGrounding=${req.body.productGrounding}`, null, function (err, data) {
            if (!err) {
                console.log(data);
                console.log(data.length);
                if (data.length === 0) {
                    res.json({
                        code: 200,
                        msg: '查询无数据',
                    })
                } else {
                    data.forEach(item => {
                        item.key = item.promotionId
                    });
                    const data1 = data
                    res.json({
                        code: 200,
                        msg: '查询成功',
                        data: data1
                    })
                }
            } else {
                res.json({
                    code: 500,
                    msg: "获取商品失败，请稍后重试",
                });
            }
        })
    },
    addData(req, res) {
        console.log(req.body);
        const reqData = req.body
        db.dbMysql(`INSERT INTO promotion (promotionId,promotionTitle,promotionDetail,offsetAmount,promotionType,startTime,endTime,productGrounding) VALUES (?,?,?,?,?,?,?,?)`
            , [null, reqData.promotionTitle, reqData.promotionDetail, reqData.offsetAmount, reqData.promotionType, reqData.startTime, reqData.endTime, reqData.productGrounding]
            , function (err, data) {
                if (data) {
                    res.json({
                        code: 200,
                        data: data,
                        msg: '添加成功',
                    })
                } else {
                    res.send({
                        code: 500,
                        msg: "服务器错误",
                    });
                }
            })
    },
    delData(req, res) {
        console.log(req.body);
        db.dbMysql(`DELETE FROM  promotion WHERE promotionId = ${req.body.id}`, null, function (err, data) {
            if (data) {
                res.json({
                    code: 200,
                    msg: '删除成功'
                })
            } else {
                res.send({
                    code: 500,
                    msg: '删除失败'
                })
            }
        })
    },
    editData(req, res) {
        console.log(req.body);
        const reqData = req.body
        db.dbMysql(`UPDATE promotion SET promotionTitle='${reqData.promotionTitle}',promotionDetail='${reqData.promotionDetail}',offsetAmount='${reqData.offsetAmount}',promotionType='${reqData.promotionType}',startTime='${reqData.startTime}',endTime='${reqData.endTime}',productGrounding=${reqData.productGrounding} WHERE PromotionId = ${reqData.PromotionId}`
            , null
            , function (err, data) {
                console.log(err);
                if (!err) {
                    res.json({
                        code: 200,
                        data: data,
                        msg: '修改成功',
                    })
                } else {
                    res.send({
                        code: 500,
                        msg: "服务器错误",
                    });
                }
            })
    },
    getProductDataId(req, res) {
        console.log(req.body);
        db.dbMysql(`SELECT * FROM productPromotion AS PR,product AS P WHERE PR.productID=P.productID AND promotionId=${req.body.promotionId}`, null, function (err, data) {
            console.log(err);
            if (!err) {
                console.log(data);
                if (data.length === 0) {
                    res.json({
                        code: 200,
                        msg: '查询无数据',
                    })
                } else {
                    data.forEach(item => {
                        item.key = item.productPromotionid
                    });
                    const data1 = data
                    res.json({
                        code: 200,
                        msg: '查询成功',
                        data: data1
                    })
                }
            } else {
                res.json({
                    code: 500,
                    msg: "获取商品失败，请稍后重试",
                });
            }
        })
    },
    delProductData(req,res){
        console.log(req.body);
        db.dbMysql(`DELETE FROM  productpromotion WHERE productPromotionid = ${req.body.id}`, null, function (err, data) {
            console.log(err);
            if (data) {
                res.json({
                    code: 200,
                    msg: '删除成功'
                })
            } else {
                res.send({
                    code: 500,
                    msg: '删除失败'
                })
            }
        })
    },
    getAllProductData(req, res) {
        db.dbMysql(`SELECT * FROM product WHERE productState=${req.body.productState}`, null, function (err, data) {
            console.log(data);
            if(data){
                data.forEach(item => {
                    item.key = item.productID
                });
                const data1 = data
                res.json({
                    code: 200,
                    msg: '查询成功',
                    data: data1
                })
            }
        })
    },
    getAppointProductData(req, res) {
        console.log(req.body);
        db.dbMysql(`SELECT * FROM product WHERE productName like "%${req.body.productName}%" AND productID like "%${req.body.productID}%" AND productBrand='${req.body.productBrand}' AND productState='${req.body.productState}'`, null, function (err, data) {
            if (!err) {
                console.log(data);
                console.log(data.length);
                if (data.length === 0) {
                    res.json({
                        code: 200,
                        msg: '查询无数据',
                    })
                } else {
                    data.forEach(item => {
                        item.key = item.productID
                    });
                    const data1 = data
                    res.json({
                        code: 200,
                        msg: '查询成功',
                        data: data1
                    })
                }
            } else {
                res.json({
                    code: 500,
                    msg: "获取商品失败，请稍后重试",
                });
            }
        })
    },
    addProductData(req, res) {
        console.log(req.body);
        db.dbMysql(`INSERT INTO productpromotion (productPromotionid,promotionId,productID) VALUES (?,?,?)`
        , [null, req.body.promotionId, req.body.productID]
        , function (err, data) {
            console.log(err);
            if (data) {
                res.json({
                    code: 200,
                    data: data,
                    msg: '添加成功',
                })
            } else {
                res.send({
                    code: 500,
                    msg: "服务器错误",
                });
            }
        })
    }
}



